package com.study.jyg.rxjava2study.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.activity.LineChartActivity2;
import com.study.jyg.rxjava2study.utils.AdapterInit;
import com.study.jyg.rxjava2study.utils.DensityUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyChartView extends RelativeLayout {

    private ChartAdapter adapter;

    private int dataSize;//排满屏幕显示多少条数据

    private int pillarWidth;//柱子宽度
    private int pillarSpacing;//柱子间隔
    private int pillarColor;//柱子颜色



    public MyChartView(Context context) {
        this(context, null, 0);
    }

    public MyChartView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }


    /**
     *
     */
    private void initView(Context context, AttributeSet attrs){
        View view = LayoutInflater.from(context).inflate( R.layout.view_chart, this, false);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        adapter = new ChartAdapter(R.layout.item_view_chart);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL));
        recyclerView.setAdapter(adapter);
        recyclerView.post(()->{
            int width = recyclerView.getWidth();
            dataSize = width/DensityUtil.sp2px(25)+1;
            List<ZZData> list = new ArrayList<>();
            for (int i = 0; i < dataSize; i++) {
                list.add(new ZZData(i, "", 40,10,false));
            }
            adapter.setNewData(list);
        });
        addView(view);
    }

    /**
     * 添加数据
     * @param data
     */
    public void addData(ZZData data){
        adapter.addData(data);
    }


    static class ChartAdapter extends BaseQuickAdapter<ZZData, BaseViewHolder>{

        private int dataPosition;//最后一条数据的position

        public ChartAdapter(int layoutResId) {
            super(layoutResId);
        }


        @Override
        protected void convert(@NotNull BaseViewHolder helper, ZZData item) {
            helper.setText(R.id.tvBottom, String.valueOf(item.getPsotion()));

            BarGraphItem viewZz = helper.getView(R.id.viewZz);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) viewZz.getLayoutParams();
            if (item.isHasData()){
                params.height = DensityUtil.dip2px(item.getHeight());
            }else {
                params.height = 0;
            }
            params.rightMargin = DensityUtil.dip2px((float) item.getSpacing()/2);
            params.leftMargin = DensityUtil.dip2px((float)item.getSpacing()/2);
            viewZz.setLayoutParams(params);
        }
    }
    static class ZZData{
        private int psotion;
        private boolean hasData;
        private String name;
        private int height;
        private int spacing;

        public ZZData(int psotion, String name, int height, int spacing, boolean hasData) {
            this.psotion = psotion;
            this.name = name;
            this.height = height;
            this.spacing = spacing;
            this.hasData = hasData;
        }

        public int getPsotion() {
            return psotion;
        }

        public void setPsotion(int psotion) {
            this.psotion = psotion;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }

        public int getSpacing() {
            return spacing;
        }

        public void setSpacing(int spacing) {
            this.spacing = spacing;
        }

        public boolean isHasData() {
            return hasData;
        }

        public void setHasData(boolean hasData) {
            this.hasData = hasData;
        }
    }
}
